package com.portal.gui.marketman;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Product;
import com.portal.logic.ProductDAO;
import com.portal.logic.SerializationUtil;
import com.portal.logic.TypeDAO;

public class EditProductScr extends JFrame {

	private JPanel contentPane;
	private JTextField txtProductName;
	private JTextField txtProductDesc;
	private JTextField txtProductPrice;
	
	private static String selectedProduct;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EditProductScr frame = new EditProductScr(selectedProduct);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @param selectedProduct 
	 */
	public EditProductScr(String selectedProduct) {
		setTitle("Edit Product");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		ProductDAO productDAO = new ProductDAO(ConnectionManager.getConnection());
		Product product = productDAO.selectByName(selectedProduct);
		
		TypeDAO typeDAO = new TypeDAO(ConnectionManager.getConnection());
		com.portal.logic.Type type = typeDAO.selectById(product.getIdType());
		
		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		btnBack.setBounds(40, 167, 97, 25);
		contentPane.add(btnBack);
		
		JLabel label = new JLabel("Product Name");
		label.setBounds(116, 38, 90, 16);
		contentPane.add(label);
		
		final JComboBox cmbProductType = new JComboBox();
		
		List<com.portal.logic.Type> types = typeDAO.selectAll();
		Iterator<com.portal.logic.Type> iterator = types.iterator();
		while (iterator.hasNext()) {
			com.portal.logic.Type type1 = (com.portal.logic.Type) iterator.next();
			cmbProductType.addItem(type1.getName());
		}
		
		System.err.println(type.getName());
		cmbProductType.setSelectedItem(type.getName());
		cmbProductType.setBounds(218, 122, 90, 22);
		
		contentPane.add(cmbProductType);
		
		txtProductName = new JTextField();
		txtProductName.setColumns(10);
		txtProductName.setBounds(218, 35, 90, 22);
		txtProductName.setText(selectedProduct); 
		contentPane.add(txtProductName);
		
		JLabel label_1 = new JLabel("Product Desc");
		label_1.setBounds(116, 67, 93, 16);
		contentPane.add(label_1);
		
		txtProductDesc = new JTextField();
		txtProductDesc.setColumns(10);
		txtProductDesc.setBounds(218, 64, 90, 22);
		txtProductDesc.setText(product.getDesc()); 
		contentPane.add(txtProductDesc);
		
		txtProductPrice = new JTextField();
		txtProductPrice.setColumns(10);
		txtProductPrice.setBounds(218, 94, 90, 19);
		String price = Integer.toString(product.getPrice());
		txtProductPrice.setText(price); 
		contentPane.add(txtProductPrice);
		
		JLabel label_2 = new JLabel("Product Price");
		label_2.setBounds(116, 96, 90, 16);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("Product Type");
		label_3.setBounds(116, 125, 75, 16);
		contentPane.add(label_3);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				ProductDAO productDAO = new ProductDAO(ConnectionManager.getConnection());
				Product product = new Product();
				product.setId(productDAO.selectMaxId()+1);
				String name = (String)txtProductName.getText();
				product.setName(name);
				String desc = (String)txtProductDesc.getText();
				product.setDesc(desc);
				String price = (String)txtProductPrice.getText();
				product.setPrice( Integer.parseInt(price));
				String type = (String)cmbProductType.getSelectedItem();
				TypeDAO typetDAO = new TypeDAO(ConnectionManager.getConnection());
				com.portal.logic.Type t = typetDAO.selectByName(type);
				product.setIdType(t.getId());
				
				try {
					SerializationUtil.serialize(product, "c:\\editProduct.out");
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			    productDAO.update(); 
			    JOptionPane.showMessageDialog(null, "Product was updated!");
			}
		});
		btnSubmit.setBounds(264, 194, 97, 25);
		contentPane.add(btnSubmit);
	}

	public static String getSelectedProduct() {
		return selectedProduct;
	}

	public static void setSelectedProduct(String selectedProduct) {
		EditProductScr.selectedProduct = selectedProduct;
	}

}
